﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace MaterialEditor
{
    public class NodeGroupBox
    {
        /// <summary>
        /// The nodes grouped in this group box
        /// </summary>
        public List<Node> nodes = new List<Node>();

        /// <summary>
        /// The name of this group box
        /// </summary>
        public string name;

        /// <summary>
        /// The area this group box takes up
        /// </summary>
        public System.Drawing.Rectangle box
        {
            get
            {
                int left = int.MaxValue,
                    top = int.MaxValue,
                    bottom = int.MinValue,
                    right = int.MinValue;

                foreach (Node n in nodes)
                {
                    left = Math.Min(n.Rect.Left - padding, left);
                    top = Math.Min(n.Rect.Top - padding / 2, top);
                    right = Math.Max(n.Rect.Right + padding, right);
                    bottom = Math.Max(n.Rect.Bottom + padding / 2, bottom);
                }

                return new System.Drawing.Rectangle(left, top, right - left, bottom - top);
            }
        }

        public System.Drawing.Color borderColor = System.Drawing.Color.Black;

        /// <summary>
        /// A rectgangle that represents the area of the group box that contains its name
        /// </summary>
        public System.Drawing.Rectangle TextBox
        {
            get
            {
                Vector2 dim = Fonts.fonts["Arial"].MeasureString(name);
                return new System.Drawing.Rectangle(box.Left, box.Top - 40, (int)dim.X, (int)dim.Y);
            }
        }

        /// <summary>
        /// the ammount of distance from the edge of the box to the closest node
        /// </summary>
        const int padding = 10;

        public NodeGroupBox(IEnumerable<Node> nodes, string name)
        {
            this.nodes.AddRange(nodes);
            this.name = name;
            foreach (Node n in nodes)
                n.group = this;
        }

        public void Update()
        {
            if (nodes.Count <= 1)
                Form1.instance.viewWindow1.groupBoxes.Remove(this);
        }

        public void Draw()
        {
            Graphics.Instance.DrawText(new Vector2(box.X, box.Y - 40), Fonts.fonts["Arial"], name, borderColor);
            Graphics.Instance.DrawHollowRect(box, borderColor);
        }
    }
}